package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        //获得当前用户的Id，查询所有地址
        //select * from address_book what userId = ? and id_delete = 0
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDeleted,0);
        List<AddressBook> addressBookList = addressBookService.list(queryWrapper);
        if (addressBookList.size()<=0){
            throw new CustomException("该用户还没有地址");
        }
        return R.success(addressBookList);
    }


    //地址簿列表
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        log.info("新增地址簿 {}",addressBook);
        //新增当前用户的地址
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        //updateWrapper.set(AddressBook::getIsDeleted,0);
        addressBookService.save(addressBook);
        return R.success("新增地址成功");
    }

    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        //1.先将当前用户所有地址设置为不是默认
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AddressBook::getIsDefault,0);
        updateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(updateWrapper);
        //2.设置当前选择的地址isDefault为默认 1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置默认值成功");
    }
    //回显
    @GetMapping("{id}")
    public R get(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AddressBook::getIsDeleted,1);
        updateWrapper.eq(AddressBook::getId,ids);
        addressBookService.update(updateWrapper);
        return R.success("地址删除成功");
    }

    public R<AddressBook> getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        if (one!=null){
            return R.success(one);
        }
        return R.error("该用户没有默认地址");
    }
}
